// pages/edit/edit.js
var util = require('../../utils/util.js')
var app = getApp()

Page({
   data: {
      input_answer_list: [],
      default_input_answer_list: {},
      name: '',
      flg: 0,
      colorArr: [//增加选项的默认颜色排序
         '#EE534F',
         '#FF7F50',
         '#FFC928',
         '#66BB6A',
         '#42A5F6',
         '#FF7F50',
         '#AA47BC',
         '#EC407A',
         '#DA70D6',
         '#FFA827',
         '#AA47BC',
         '#EE534F',
         '#42A5F6',
         '#66BB6A',
         '#FFC928',
         '#42A5F6',
         '#5C6BC0',
      ],
      userId:'',
      type:0,
      hearttackId:0
   },

   onLoad: function (options) {
      var that = this, input_answer_list = [], obj = {}, myJuedin = wx.getStorageSync('myJuedin'), arr = [], all = wx.getStorageSync('all'), default_input_answer_list = that.data.default_input_answer_list, num = wx.getStorageSync('num');
     
      that.setData({
         type:options.type,
         userId:options.userId,
         name: options.name,
      })
      if (options.hearttackId != 0) {
         //个人决定编辑跳过来的
         that.setData({
            
            hearttackId:options.hearttackId 
         })
         that.getData(options.hearttackId)
      }
   },

   
//查询真心话或者大冒险列表（个人）
getData(id) {
   var that = this
   wx.request({
     url: app.mainurl + 'hearttacklist/getList', //仅为示例，并非真实的接口地址
     data: {
      hearttackId:id,
     },
     method: 'POST',
     header: {
       'content-type': 'application/x-www-form-urlencoded'
     },
     success(res) {
       var datas = res.data.data
      
       var default_input_answer_list={}
       default_input_answer_list.name = that.data.name
       default_input_answer_list.userId = that.data.userId
       default_input_answer_list.type = that.data.type
       default_input_answer_list.id = that.data.hearttackId
       var arr= []
       for(var i=0;i<datas.length;i++){
          var arrjson={}
          arrjson.name=datas[i].name
         arr.push(arrjson)
      }
     
      default_input_answer_list.hearttacklist=arr

       that.setData({
          input_answer_list: arr,
          default_input_answer_list:default_input_answer_list
      })

     }
   })
 },
   //小决定的名称
   checkQuestion(e) {
      var that = this, val = e.detail.value, default_input_answer_list = that.data.default_input_answer_list;
      default_input_answer_list.name = val
      default_input_answer_list.userId = that.data.userId
      default_input_answer_list.type = that.data.type
      if(that.data.hearttackId>0){
         default_input_answer_list.id = that.data.hearttackId
      }
      that.setData({
         name: val,
         default_input_answer_list: default_input_answer_list
      })
   },

   //小决定选项
   checkAnswer(e) {
      var that = this, val = e.detail.value, index = e.currentTarget.dataset.index, input_answer_list = that.data.input_answer_list, default_input_answer_list = that.data.default_input_answer_list;

      for (let i in input_answer_list) {
         if (index == i) {
            input_answer_list[i].name = val
        
         }
      }
      default_input_answer_list.hearttacklist = input_answer_list;
      that.setData({
         input_answer_list: input_answer_list,
         default_input_answer_list: default_input_answer_list
      })
   },

   //增加
   addAnswer() {
      var that = this, input_answer_list = that.data.input_answer_list, colorArr = that.data.colorArr, obj = {};
      if (input_answer_list.length == 200) {
         wx.showToast({
            title: '选项长度最多200项',
            icon:'none',
            mask:false
         })
         return;
      }
      obj = { name: '' };
      input_answer_list.push(obj);
      that.setData({
         input_answer_list: input_answer_list
      })
   },
  
   //删除
   subAnswer(e) {
      var that = this, index = e.currentTarget.dataset.index, input_answer_list = that.data.input_answer_list, default_input_answer_list = that.data.default_input_answer_list, colorArr = that.data.colorArr;
      for (let i in input_answer_list) {
         if (i == index) {
            input_answer_list.splice(i, 1);
            break;
         }
      }

     

      default_input_answer_list.hearttacklist = input_answer_list;
      that.setData({
         input_answer_list: input_answer_list,
         default_input_answer_list: default_input_answer_list
      })
   },

   //保存
   saveQA() {
      var that = this, myJuedin = wx.getStorageSync('myJuedin'), default_input_answer_list = that.data.default_input_answer_list, input_answer_list = that.data.input_answer_list, all = wx.getStorageSync('all'), arr = [];

      if (that.data.name == '') {
         wx.showToast({
            title: '名称不能为空',
            icon: 'none',
            mask: false
         })
      } else {
         for (let y in input_answer_list) {
            if (input_answer_list[y].name == '') {
               wx.showToast({
                  title: '选项不能为空',
                  icon: 'none',
                  mask: false
               })
               return;
            }
         }

         if (input_answer_list.length < 2) {
            wx.showToast({
               title: '选项最少填2个',
               icon: 'none',
               mask: false
            })
            return;
         }
       

         wx.request({
            url: app.mainurl + 'hearttack/update', //仅为示例，并非真实的接口地址
            data:JSON.stringify(default_input_answer_list),
            method: 'POST',
            header: {
              'content-type': 'application/json'
            },
            success(res) {
               wx.showToast({
                  title: '保存成功',
                  icon: 'none',
                  duration: 2000
                })
                setTimeout(() => {    
                  wx.navigateBack({
                    delta: 1
                  })
                }, 2000); 
            }
          })
        
      }
   },

   onUnload: function () {
      var that = this, flg = that.data.flg, myJuedin = wx.getStorageSync('myJuedin'), all = wx.getStorageSync('all');

      if (flg == 1) {
         myJuedin.splice(myJuedin.length - 1, 1);
         wx.setStorageSync('myJuedin', myJuedin);
      }

      function rep(arr) {
         var result = [];
         var obj = {};
         for (var i = 0; i < arr.length; i++) {
            if (!obj[arr[i].id]) {
               result.push(arr[i]);
               obj[arr[i].id] = true;
            }
         }
         return result;
      }
      var a = rep(myJuedin), b = rep(all);
      wx.setStorageSync('myJuedin', a);
      wx.setStorageSync('all', b);
   },

   onShareAppMessage: function () {
      let that = this;
      mta.Event.stat("share", { 'time': '1' });
      var picNum = Math.floor(Math.random() * 4 + 1);//获取1-4的随机数，用于随机展示分享图片
      return {
         title: util.isNull(app.globalData.shareTitle) ? ("一起来玩'" + app.globalData.title + "'吧") : app.globalData.shareTitle,
         path: '/pages/index/index',
         success: function (res) {
            console.log('成功进入分享==========', res);

         },
         fail: function (res) {
            console.log('进入分享失败==========', res);
         }
      }
   }
})